package com.example.swd1.gudong.main.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.main.MainActivity;
import com.example.swd1.gudong.main.user.historyrecord.HistoryRecordActivity;
import com.example.swd1.gudong.main.user.myinfo.MyInfoActivity;
import com.example.swd1.gudong.main.user.mymedal.UserMedalActivity;
import com.example.swd1.gudong.main.user.myrecord.UserRecordActivity;
import com.example.swd1.gudong.main.user.runningrank.RunningRankActivity;
import com.example.swd1.gudong.main.user.systemoption.SystemOptionActivity;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class UserFragment extends Fragment implements View.OnClickListener {
    private ImageView iv_me_icon;
    private TextView tv_me_set;
    private RelativeLayout rl_history_record;
    private RelativeLayout ll_user_friend_rank;
    private LinearLayout ll_user_record;
    private LinearLayout ll_user_medal;

    @Override
    public void onClick(View v) {
        if (v == iv_me_icon) {
            Intent intent = new Intent(getContext(), MyInfoActivity.class);
            getContext().startActivity(intent);
            return;
        }
        if (v == rl_history_record) {
            Intent intent = new Intent(getContext(), HistoryRecordActivity.class);
            getContext().startActivity(intent);
            return;
        }
        if (v == tv_me_set) {
            Intent intent = new Intent(getContext(), SystemOptionActivity.class);
            getContext().startActivity(intent);
            return;
        }
        if (v == ll_user_friend_rank) {
            Intent intent = new Intent(getContext(), RunningRankActivity.class);
            getContext().startActivity(intent);
            return;
        }
        if (v == ll_user_record) {
            Intent intent = new Intent(getContext(), UserRecordActivity.class);
            startActivity(intent);
            return;
        }
        if (v == ll_user_medal) {
            Intent intent = new Intent(getContext(), UserMedalActivity.class);
            startActivity(intent);
            return;
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, container, false);
        iv_me_icon = (ImageView) view.findViewById(R.id.iv_me_icon);
        rl_history_record = (RelativeLayout) view.findViewById(R.id.rl_history_record);
        tv_me_set = (TextView) view.findViewById(R.id.tv_me_set);
        ll_user_friend_rank = (RelativeLayout) view.findViewById(R.id.ll_user_friend_rank);
        ll_user_record = (LinearLayout) view.findViewById(R.id.ll_user_record);
        ll_user_medal = (LinearLayout) view.findViewById(R.id.ll_user_medal);
        initHeadIcon();

        iv_me_icon.setOnClickListener(this);
        rl_history_record.setOnClickListener(this);
        tv_me_set.setOnClickListener(this);
        ll_user_friend_rank.setOnClickListener(this);
        ll_user_record.setOnClickListener(this);
        ll_user_medal.setOnClickListener(this);
        return view;
    }

    private void initHeadIcon() {
        Bitmap iconBitmap = BitmapFactory.decodeFile(getActivity().getFilesDir().getAbsolutePath() + "/" + getString(R.string.user_head_name));
        if (iconBitmap != null) {
            iv_me_icon.setImageBitmap(iconBitmap);
        }
    }
    /*-begin-20160907-shile.wu-add-for-task2781345-*/

    /*
     *resolve the bug that when this fragment resume,the toolbar is visible
     */
    @Override
    public void onResume() {
        super.onResume();
        initHeadIcon();
        if (((MainActivity) getActivity()).getCurrentFragment().equals(this)) {
            ((MainActivity) getActivity()).showUserMenu();
        }
    }
    /*-end-20160907-shile.wu-add-for-task2781345-*/

}
